
public class EZLogic {

	static TaskOrganizer taskOrganizer;
	static UI ui;
	static NewParser parser;
	static int startDate;
	static int startPrio;
	
	private static void executeInput(String input) throws Exception {

		InputData inputData = parser.parseUserInput(input);
		Command command = inputData.command;
		boolean isTimed;
		String[][] tasks;
		int date, time, prio, id;
		String desc;
		
		switch (command) {

		case ADD: // add

			isTimed = inputData.data.taskIsTimed;;
			desc = inputData.data.desc;

			if (isTimed) {
				
				TimedTaskData timedTaskData = (TimedTaskData) inputData.data;
				
				date = timedTaskData.date;
				time = timedTaskData.time;
				
				
				taskOrganizer.addTimedTask(desc, date, time);
				tasks = taskOrganizer.getTimedTasks(startDate, -1);
				ui.displayCalList(tasks);
			} else {	
				
				NontimedTaskData nontimedTaskData = (NontimedTaskData) inputData.data;
				
				prio = nontimedTaskData.prio;
				
				taskOrganizer.addNontimedTask(desc, prio);
				tasks = taskOrganizer.getNontimedTasks(startPrio, -1);
				ui.displayPrioList(tasks);
			}
			break;

		case EDIT: // edit

			isTimed = inputData.data.taskIsTimed;
			desc = inputData.data.desc;
			id = inputData.data.id;
			
			if (isTimed) {
				

				TimedTaskData timedTaskData = (TimedTaskData) inputData.data;
				
				date = timedTaskData.date;
				time = timedTaskData.time;
				
				taskOrganizer.editTimed(id, desc, date, time);
				tasks = taskOrganizer.getTimedTasks(startDate, -1);
				ui.displayCalList(tasks);
			} else {
				NontimedTaskData nontimedTaskData = (NontimedTaskData) inputData.data;
				
				prio = nontimedTaskData.prio;				taskOrganizer.editNontimed(id, desc, prio);
				
				tasks = taskOrganizer.getNontimedTasks(startPrio, -1);				
				ui.displayPrioList(tasks);
			}

			break;

		case DELETE: // delete
			
			isTimed = inputData.data.taskIsTimed;
			id = inputData.data.id;
			
			if(isTimed){
				taskOrganizer.removeTimedTask(id);
				tasks = taskOrganizer.getTimedTasks(startDate, -1);
				ui.displayCalList(tasks);
			}
			else{
				taskOrganizer.removeNontimedTask(id);
				tasks = taskOrganizer.getNontimedTasks(startPrio, -1);
				ui.displayPrioList(tasks);
			}
			break;

		case UNDO: // undo
			break;

		case GOTO: // goto
			isTimed = inputData.data.taskIsTimed;
			
			if(isTimed){
				TimedTaskData timedTaskData = (TimedTaskData) inputData.data;
				
				startDate = timedTaskData.date;
				
				tasks = taskOrganizer.getTimedTasks(startDate, -1);
				ui.displayCalList(tasks);
			}
			else{
				NontimedTaskData nontimedTaskData = (NontimedTaskData) inputData.data;
				
				startPrio = nontimedTaskData.prio;
				
				tasks = taskOrganizer.getNontimedTasks(startPrio, -1);
				ui.displayPrioList(tasks);
			}
			break;
			
		case SEARCH: // search
			String keywords = inputData.data.keywords;
			tasks = taskOrganizer.getNontimedTasksOnKeywords(keywords);
			ui.displayPrioList(tasks);
			tasks = taskOrganizer.getTimedTasksOnKeyWords(keywords);
			ui.displayCalList(tasks);
			break;
			
		case COMPLETE: // complete task
			isTimed = inputData.data.taskIsTimed;
			id = inputData.data.id;
			if(isTimed){
				taskOrganizer.completeTimedTask(id);
				tasks = taskOrganizer.getTimedTasks(startDate, -1);
				ui.displayCalList(tasks);
			}
			else{
				taskOrganizer.completeNontimedTask(id);
				tasks = taskOrganizer.getNontimedTasks(startPrio, -1);
				ui.displayPrioList(tasks);
			}
			break;
			
		case CLEAR: // clear all completed tasks
			taskOrganizer.clearCompletedTasks();
			tasks = taskOrganizer.getNontimedTasks(startPrio, -1);
			ui.displayPrioList(tasks);
			tasks = taskOrganizer.getTimedTasks(startDate, -1);
			ui.displayCalList(tasks);
			break;
		default:
			assert false : command;
			break;
		}
	}

	public static void main(String[] args) throws Exception {

		ui = new UI();
		parser = new NewParser();
		taskOrganizer = new TaskOrganizer();
		startDate = 19000101; //default value
		startPrio = 3; //default value
		String[][] tasks;
		tasks = taskOrganizer.getTimedTasks(startDate, -1);
		ui.displayCalList(tasks);
		tasks = taskOrganizer.getNontimedTasks(startPrio, -1);
		ui.displayPrioList(tasks);

		int counter = 1;
		while (true) {
			String input = ui.getUserInput();
			if (!input.equals("") && counter == 1) {
				ui.displayLog(input);
				try{
					executeInput(input);
				} catch(Exception e){
					//tell user about error?
					ui.displayLog("- "+e.getMessage());
				}
				counter = 0;				
			} else {
				counter = 1;
			}
			if (input.equals("quit")) {
				break;
			}
		}
		
	}
}
